﻿// <copyright file="DetailsPage.xaml.cs" company="Microsoft Corporation">
// Copyright (c) 2010 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-09-13</date>
// <summary>DetailsPage code behind</summary>

namespace NYTMovieReviews
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Tasks;
    using System.Linq;
    using Microsoft.Phone.Shell;
    using NYTimes;

    /// <summary>
    /// Movie Review Details page 
    /// </summary>
    public partial class DetailsPage : PhoneApplicationPage
    {
        private int index;

        private MovieReviewsByKeyword reviews;
        
        /// <summary>
        /// Initializes a new instance of the DetailsPage class.
        /// </summary>
        public DetailsPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string selectedIndex = string.Empty;

            var viewModel = Application.Current.Resources["ViewModelDataSource"] as ViewModel;

            string pivot = string.Empty;
            if (NavigationContext.QueryString.TryGetValue("pivot", out pivot))
            {
                if (pivot == string.Empty)
                {
                    this.reviews = (from pivotItem in viewModel.Pivots
                                    where pivotItem.Reviews.Order == null
                                    select pivotItem.Reviews).FirstOrDefault();
                }
                else
                {
                    this.reviews = (from pivotItem in viewModel.Pivots
                                    where pivotItem.Reviews.Order.ToString() == pivot
                                    select pivotItem.Reviews).FirstOrDefault();
                }
            }

            this.reviews.Results.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Results_CollectionChanged);

            if (NavigationContext.QueryString.TryGetValue("index", out selectedIndex))
            {
                this.index = int.Parse(selectedIndex);
            }
            else
            {
                object lastIndex = 0;
                if (PhoneApplicationService.Current.State.TryGetValue("Index", out lastIndex))
                {
                    this.index = (int)lastIndex;
                }
            }

            this.UpdateDataContext();
        }

        private void Results_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            this.UpdateDataContext();
        }

        private void UpdateDataContext()
        {
            if (this.index < this.reviews.Results.Count)
            {
                this.DataContext = this.reviews.Results[this.index];
            }
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            if (PhoneApplicationService.Current.State.ContainsKey("Index"))
            {
                PhoneApplicationService.Current.State.Remove("Index");
            }

            PhoneApplicationService.Current.State.Add("Index", this.index);
        }

        /// <summary>
        /// Show the article with the web browser
        /// </summary>
        /// <param name="sender">the button</param>
        /// <param name="e">the routed event arguments</param>
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var button = sender as Button;
            
            var uri = button.Tag as Uri;

            WebBrowserTask task = new WebBrowserTask();

            task.URL = uri.ToString();

            task.Show();
        }
    }
}